package com.demo.activity;



import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.demo.common.model.Activity;
import com.demo.common.model.FormConfig;
import com.demo.common.model.FormRecord;
import com.demo.util.ExcelUtil;
import com.jfinal.core.Controller;
import com.jfinal.json.Json;
import com.jfinal.kit.JsonKit;

public class ActivityController<V> extends Controller {
	static ActivityService activityService = ActivityService.me;
	
	public void index(){
		List activityList = activityService.getActivityList();
		setAttr("activityList", activityList);
		render("index.html");
	}
	
	public void activity_list(){
		
		renderJson();
	}
	
	public void add_activity(){
		
		
		String name = getPara("name");
		String address = getPara("address");
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		
		String names = getPara("names");
		String types = getPara("types");
		String[] namesss = names.split(",");
		String[] typesss = types.split(",");
		
		String registrationDeadline = getPara("registrationDeadline");
		activityService.saveActivityInfo(name,address,startTime,endTime,registrationDeadline,namesss,typesss);
//		List<String> value = new ArrayList<String>();
//		value.add("201424133201");
//		value.add("林庭阳");
//		String json = JsonKit.toJson(value);  
//		new FormRecord().set("value", json).save();
		

		renderJson();
	}
	
	public void delete_activity(){
		
		
		String activityId = getPara();
		
		activityService.deleteActivity(activityId);
//		List<String> value = new ArrayList<String>();
//		value.add("201424133201");
//		value.add("林庭阳");
//		String json = JsonKit.toJson(value);  
//		new FormRecord().set("value", json).save();
		
		redirect("/");
	}
	
	public void registration(){
		
		String activityId = getPara();
		
		
		List formConfigs = activityService.registration(activityId);
		setAttr("formConfigs", formConfigs);
	}
	
	public void do_registration(){
		
		String activityId = getPara();
		
		String[] paraValues = getParaValues("input");
		List<String> list= new ArrayList<String>();
		for(String paraValue : paraValues){
			
			list.add(paraValue);
		}
		String json = JsonKit.toJson(list);
		activityService.doRegistration(activityId,json);
		
		renderJson();
	}
	
	public void show_registration_list(){
		
		String activityId = getPara();
		
		Activity activity = activityService.getActivity(activityId);
		List<String> titles = activityService.getRegistrationTitles(activityId);
		List<List> rows = activityService.getRegistrationContents(activityId);
		
		setAttr("activity", activity);
		setAttr("titles", titles);
		setAttr("rows", rows);
		
		render("registration_list.html");
//		renderJson();
		
	}
	
	public void export_registration_list() throws IOException {
		
		String activityId = getPara();
		
		List<String> titles = activityService.getRegistrationTitles(activityId);
		List<List> rows = activityService.getRegistrationContents(activityId);
		ExcelUtil.export(titles,rows,"excel.xls");
		renderFile("excel.xls");
	}

}
